<?php

namespace app\admin\model;

use think\Db;
use think\Model;


class Article extends Model
{

    

    //数据库
    protected $connection = 'database';
    // 表名
    protected $name = 'article';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'createdata_text',
        'publishdata_text',
        'recommenddata_text',
        'hotdata_text',
        'topdata_text',
        'publishtime_text',
        'status_text',
        'coverimages_text'
    ];

    protected static function init()
    {
        self::beforeInsert(function ($row){
            $addData = $row->getData();
            if(!isset($addData['resource']) || empty($addData['resource'])){
                if(!empty($addData['site_id'])){
                    if(!empty($addData['user_id'])){
                        $group_name = Db::name('user')->alias('u')->where('u.id',$addData['user_id'])->join('user_group ug','u.group_id=ug.id')->value('ug.name');
                    }else{
                        $group_name = "站长";
                    }
                    $sitename = Db::name('user')->where('id',$row->site_id)->value('sitename');
                    if($group_name){
                        $group_name="-".$group_name;
                    }
                    $row->resource = $sitename.$group_name;
                }else{
                    $row->resource = '系统发布';
                }
            }
        });
    }

    public function getCreatedataList()
    {
        return ['1' => __('Createdata 1'), '2' => __('Createdata 2')];
    }

    public function getPublishdataList()
    {
        return ['1' => __('Publishdata 1'), '2' => __('Publishdata 2')];
    }

    public function getRecommenddataList()
    {
        return ['1' => __('Recommenddata 1'), '2' => __('Recommenddata 2')];
    }

    public function getHotdataList()
    {
        return ['1' => __('Hotdata 1'), '2' => __('Hotdata 2')];
    }

    public function getTopdataList()
    {
        return ['1' => __('Topdata 1'), '2' => __('Topdata 2')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }


    public function getCreatedataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createdata']) ? $data['createdata'] : '');
        $list = $this->getCreatedataList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPublishdataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['publishdata']) ? $data['publishdata'] : '');
        $list = $this->getPublishdataList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getRecommenddataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['recommenddata']) ? $data['recommenddata'] : '');
        $list = $this->getRecommenddataList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getHotdataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['hotdata']) ? $data['hotdata'] : '');
        $list = $this->getHotdataList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTopdataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['topdata']) ? $data['topdata'] : '');
        $list = $this->getTopdataList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPublishtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['publishtime']) ? $data['publishtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function getCoverimagesTextAttr($value,$data){
        $value = $value ? explode(',',$value) : (isset($data['coverimages']) ? explode(',',$data['coverimages']) : '');;
        if(is_array($value)){
            foreach ($value as $k=>$v){
                $value[$k] = fixUrl($v);
            }
        }
        return $value;
    }

    protected function setPublishtimeAttr($value)
    {
        return $value === '' ? time() : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function site()
    {
        return $this->belongsTo('User', 'site_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function category()
    {
        return $this->belongsTo('Category', 'category_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function _list($field='*',$where=[],$limit=15,$order='id desc'){
        $ret = $this->field($field)->where($where)->order($order)->paginate($limit);
        return $ret;
    }
}
